package com.ktwlsoft.archivemanage.controller.study;

import com.ktwlsoft.archivemanage.domain.business.businessService.study.StudySubjectService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.study.subject.SaveSubjectRequest;
import com.ktwlsoft.archivemanage.payload.study.subject.UpDownRequest;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/study/subject")
public class StudySubjectController {

    private final StudySubjectService subjectService;

    public StudySubjectController(StudySubjectService subjectService) {
        this.subjectService = subjectService;
    }

    @PostMapping("/manage/save")
    public ResponseEntity<?> manageSave(@RequestBody SaveSubjectRequest request) {
        return ResponseEntity.ok(Result.ok(subjectService.save(request, "MANAGE")));
    }

    @PostMapping("/user/save")
    public ResponseEntity<?> userSave(@RequestBody SaveSubjectRequest request) {
        return ResponseEntity.ok(Result.ok(subjectService.save(request, "USER")));
    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> deleteSubject(@RequestParam Long id) {
        subjectService.delete(id);
        return ResponseEntity.ok(Result.ok(id));
    }

    @GetMapping("/manage/list")
    public ResponseEntity<?> getList(@RequestParam Integer pn,
                                     @RequestParam Integer pageSize,
                                     @RequestParam(required = false) String keywords,
                                     @ApiParam("DESC, ASC") @RequestParam(required = false) String order) {
        return ResponseEntity.ok(Result.ok(subjectService.getList(pn, pageSize, keywords, "MANAGE", order)));
    }

    @GetMapping("/user/list")
    public ResponseEntity<?> getUserList(@RequestParam Integer pn, @RequestParam Integer pageSize,
                                         @RequestParam(required = false) String keywords,
                                         @ApiParam("DESC, ASC") @RequestParam(required = false) String order) {
        return ResponseEntity.ok(Result.ok(subjectService.getList(pn, pageSize, keywords, "USER", order)));
    }

    @PutMapping("/updown")
    public ResponseEntity<?> upDown(@RequestBody UpDownRequest upDownRequest) {
        return ResponseEntity.ok(Result.ok(subjectService.updown(upDownRequest.getId(), upDownRequest.getState())));
    }

    @GetMapping("/get")
    public ResponseEntity<?> getById(@RequestParam Long id) {
        return ResponseEntity.ok(Result.ok(subjectService.getById(id)));
    }
}
